//
//  FPImageView.m
//  FootPrint
//
//  Created by alex tao on 3/21/13.
//  Copyright (c) 2013 alex tao. All rights reserved.
//

#import "FPImageView.h"

@implementation FPImageView

- (void) internalInit
{
    [super internalInit];
    _imgView.image = [UIImage imageNamed:@"default_network_img.png"];
    
    self.layer.cornerRadius = 5;
//    self.layer.borderColor = [UIColor whiteColor].CGColor;
//    self.layer.borderWidth = 1;
    self.clipsToBounds = YES;
    
    self.showASAP = YES;
}

+ (NSArray*) groupNames {
    static NSArray * sImgNameArr = nil;
    if (nil == sImgNameArr) {
        sImgNameArr = [[NSArray arrayWithObjects:@"imgCache1", @"imgCache2", @"imgCache3", @"imgCache4", @"imgCache5", nil] retain];
    }
    return sImgNameArr;
}

+ (NSString*) randomGroupName {
    static NSUInteger sNameIdx = 0;
    NSArray * arr = [FPImageView groupNames];
    if (sNameIdx >= arr.count) sNameIdx= 0;
    return [arr objectAtIndex:sNameIdx++];
}

- (void) setLocalImageName:(NSString*)name
{
    self.imgUrl = name;
    [self goFetching];
}

- (void) setSysImageWithId:(long long)sysId
{
    NSString * sysImg = [NSString stringWithFormat:@"sys_%04lld", sysId];
    [self setLocalImageName:sysImg];
}

- (void)loadAsyncImage:(NSString*)url placeHolderImage:(UIImage*)placeHolder andLoadFailedImage:(UIImage*)failedImage
{
    if (![url hasPrefix:@"http://"]) {
        url = [NSString stringWithFormat:IMGURL_PREV, url];
    }
    
    self.group = [FPImageView randomGroupName];
    
    //self.placeHolderImg = placeHolder ? placeHolder : [UIImage imageNamed:@"default_network_img.png"];
    self.failImg = failedImage;

    self.imgUrl = url;
    [self goFetching];
}

- (void)loadAsyncImage:(NSString*)url placeHolderImage:(UIImage*)placeHolder
{
    [self loadAsyncImage:url placeHolderImage:placeHolder andLoadFailedImage:[UIImage imageNamed:@"default_network_img.png"]];
}

- (void)loadAsyncImage:(NSString*)url
{
    [self loadAsyncImage:url placeHolderImage:nil];
}

- (BOOL)isComplate
{
    return _penddingStat == ePenddingComplete;
}

@end
